package com.storage.mongo;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;


import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.query.Query;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class CharityForum {
	

	Morphia morp;
	Mongo m; 
	DB db; 
	DBCollection topicColl; 
	Datastore ds;
	
	@SuppressWarnings("deprecation")
	CharityForum() throws UnknownHostException, MongoException
	{
		morp = new Morphia();
		m = new Mongo( "localhost" , 27017 );
		//drop db
		//m.dropDatabase("mydb");
		db = m.getDB("mydb");
		morp.map(ETopic.class).map(EComments.class);
		ds = morp.createDatastore("mydb");
	}
	private String charityName;
	private String charityDescription;
	
	@Embedded
	private Edonate donate;
	private String totalAmount;
	private String createdUser;
	private String communityID;
	private String createdDate;
	
	public void getallrecords()
	{

		List<Echarity> cf = ds.find(Echarity.class).asList();

		for(Echarity c : cf)
		{
			System.out.print(c.getId());
			System.out.print(c.getCharityName());
			System.out.print(c.getCharityDescription());
			System.out.print(c.getCommunityID());
			System.out.print(c.getCreatedDate());
			System.out.print(c.getCreatedUser());
			System.out.print(c.getDonate().getAmount());
			System.out.println(c.getDonate().getDonatedBy());
		}
	}
	
	public void createCharity(String charityName,String charityDescription,String createdUser,String amount,String communityID) throws UnknownHostException, MongoException 
	{
		Echarity c = new Echarity();

		c.setCharityName(charityName);
		c.setCharityDescription(charityDescription);
		c.setCreatedUser(createdUser);
		c.setCommunityID(communityID);
		c.setTotalAmount(amount);
		c.setCreatedDate(new Date().toString());
		
		ds.save(c);
		System.out.println("Charity created successfully");
		
	}
	
	public Query<Echarity> getCharityDetails(String charityName)
	{
		
		Query<Echarity> query= ds.createQuery(Echarity.class).field("charityName").equal(charityName);
		for (Echarity c : query)
		{
			System.out.print(c.getCharityName());
			System.out.println(c.getCharityDescription());
		}
		System.out.print("out");
		return query;
	}
	
	public List<Echarity> getParticularCharity(String charityID)
	{
		Echarity charity = new Echarity();
		charity.setId(new ObjectId(charityID));
		
		List<Echarity> list = ds.find(Echarity.class,"Id",charity.getId()).asList();

		//Query<Echarity> query= ds.createQuery(Echarity.class).field("Id").equal(charityID);
		for (Echarity c : list)
		{
			System.out.print(c.getCharityName());
			System.out.println(c.getCharityDescription());
		}
		return list;
	}
	
	
	public void donateCharity(String charityName,int donatedAmount,String donatedBy,String communityID) throws UnknownHostException, MongoException
	{
		System.out.println("xxxxxxxxxx"+charityName+donatedAmount+donatedBy+communityID);
		Echarity c = new Echarity(new Edonate(donatedAmount,donatedBy,communityID));
		c.setCharityName(charityName);
		ds.save(c);
		System.out.println("Charity donated successfully");
	}
	
	
	public List<Echarity> listCharity()
	{
		List<Echarity> cf = ds.find(Echarity.class).asList();

		for(Echarity c : cf)
		{
//			System.out.print(c.getId());
//			System.out.print(c.getCharityName());
//			System.out.print(c.getCharityDescription());
//			System.out.print(c.getCommunityID());
//			System.out.print(c.getCreatedDate());
//			System.out.print(c.getCreatedUser());
//			System.out.print(c.getDonate().getAmount());
//			System.out.println(c.getDonate().getDonatedBy());
		}
		return cf;
	}
	
	public int getTotalDonation(String charityName)
	{
		int totalAmount=0;
		
		Query<Echarity> query= ds.createQuery(Echarity.class).field("charityName").equal(charityName);
		for (Echarity c : query)
		{
			if((c.getTotalAmount()) !=null)
			{
				totalAmount = Integer.parseInt(c.getTotalAmount());
			}
			totalAmount= totalAmount + (c.getDonate().getAmount());
		}
		return totalAmount;
	}
	
	public void cleardatabase() throws UnknownHostException, MongoException
	{
		morp = new Morphia();
		m = new Mongo( "localhost" , 27017 );
		//drop db
		m.dropDatabase("mydb");
	}
	
	
}
